using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SpriteSpawner : MonoBehaviour {
	
	public List<GameObject> m_model;
	public float m_TimeToSpawn = 0f;
	
	private float m_NextSpawn = 0;
	private int m_ObjectCount = 0;
	
	public int m_MaxObject = 10000;
	
	private float[] layer = new float[4];
	
	// Use this for initialization
	void Start () 
	{
		m_NextSpawn = m_TimeToSpawn;
		
		layer[0] = 0;
		layer[1] = 100;
		layer[2] = 200;
		layer[3] = 300;
	}
	
	// Update is called once per frame
	void Update() 
	{	
		if( m_NextSpawn < 0f && m_ObjectCount < m_MaxObject )
		{
			m_NextSpawn = m_TimeToSpawn;
			m_ObjectCount++;
				
			//Spawn new go
			
			int nModel = Random.Range(0,m_model.Count);
			
			GameObject model = m_model[ Random.Range(0,m_model.Count)];
			GameObject go = (GameObject)Instantiate( (Object)model );		
			
			go.SetActive( true );
			
			//go.GetComponent<MeshFilter>().mesh.CombineMeshes( combine, true );
			
			int nlayer = Random.Range( 0,1 );
			
			if( nModel == 0 )
			{
				if( nlayer == 1 )
					nlayer = 2;
				
			}
			else
			{
				if( nlayer == 0 )
					nlayer = 1;
				else
					nlayer = 3;
							
			}
				
			
			Vector3 pos = new Vector3( Random.Range(0,1280), Random.Range(0,-720), nlayer );
			
			go.transform.parent = transform.parent;
			go.transform.localPosition = pos;
		}
		else
		{
			m_NextSpawn -= Time.deltaTime;
		}
	}
	
	void OnGUI()
	{
		GUI.TextField( new Rect (10,10,150,25), "Sprite Number : " + m_ObjectCount );
	}
}
